# -*- coding: utf-8 -*-
# @Author  : Lison Song
# @Time    : 2023/3/23 下午6:45
from applications import create_app
from flask_migrate import Migrate
from applications.extensions import db
from applications.configs.config import BaseConfig
import logging
from logging.handlers import RotatingFileHandler
from flask import request
app = create_app()

# create logger

migrate = Migrate(app, db)

@app.before_request
def log_each_request():
    app.logger.info(
        '{} {} {} {}'.format(request.remote_addr, request.method, request.path, request.query_string))


if __name__ == '__main__':
    app.config["SQLALCHEMY_POOL_RECYCLE"] = BaseConfig.SQLALCHEMY_POOL_RECYCLE
    handler = RotatingFileHandler('logs/chsdc.log', maxBytes=102400000,
                                  backupCount=9, encoding='UTF-8')
    formatter = logging.Formatter(
        '%(asctime)s %(levelname)s P[%(process)d] T[%(thread)d] %(lineno)sL@%(filename)s %(funcName)s:'
        ' %(message)s')
    handler.setFormatter(formatter)
    handler.setLevel(logging.INFO)
    app.logger.addHandler(handler)
    app.run(debug=True, threaded=True, host=BaseConfig.FLASK_RUN_HOST, port=BaseConfig.FLASK_RUN_PORT)